<?php
/**
 * +----------------------------------------------------------------------
 * | 模型
 * +----------------------------------------------------------------------
 *                      _ooOoo_
 *                     o8888888o            | AUTHOR: 杜云
 *                     88" . "88            | EMAIL: 987772927@qq.com
 *                     (| -_- |)            | QQ: 987772927
 *                     O\  =  /O            | WECHAT: 987772927
 *                  ____/`---'\____
 *                .'  \\|     |//  `.
 *               /  \\|||  :  |||//  \
 *              /  _||||| -:- |||||-  \
 *              |   | \\\  -  /// |   |
 *              | \_|  ''\-/''  |   |
 *              \  .-\__  `-`  ___/-. /
 *            ___`. .'  /-.-\  `. . __
 *         ."" '<  `.___\_<|>_/___.'  >'"".
 *        | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *        \  \ `-.   \_ __\ /__ _/   .-` /  /
 *   ======`-.____`-.___\_____/___.-`____.-'======
 *                      `=-='
 * +----------------------------------------------------------------------
 */
declare (strict_types=1);
namespace App\Model;
use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Database\Model\SoftDeletes;
class Menus extends BaseModel
{
    use SoftDeletes;
    protected $table = 'menus';
    protected $fillable = [
        'pid',
        'title',
        'icon',
        'path',
        'component',
        'apiurl',
        'type',
        'name',
        'remark',
        'sort',
        'tag',
        'hidden',
        'hidden_breadcrumb',
        'fullpage',
        'status',
        'created_user',
        'created_at',
        'updated_at',
    ];

    protected $casts = [
        'pid'   => 'integer',
        'sort'   => 'integer',
        'status'   => 'integer',
        'hidden'   => 'integer',
        'hidden_breadcrumb'   => 'integer',
        'fullpage'   => 'bool',
    ];

    protected $hidden = ['created_user', 'created_at', 'updated_at','deleted_at'];

    public static function getMenuList($user = null,$type = ['menu','iframe','link']):array
    {
        $_authList = [];
        if(!$user) return $_authList;
        if ($user->account == config('super_admin')){
            $_authList = self::whereIn('type',$type)->where(['status'=>1])->orderBy('pid','asc')->orderBy('sort','asc')->orderBy('id','desc')->get()->toArray();
        }else{
            foreach ($user->roles()->get() as $key => $item) {
                foreach ($item->menus()->whereIn('type',$type)->where(['status'=>1])->orderBy('pid','asc')->orderBy('sort','asc')->orderBy('id','desc')->get() as $ke => $it) {
                    $_authList[$it['id']] = $it;
                }
            }
        }
        return ListToTree($_authList);
    }
}
